﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SnmpForm.SnmpWrapper
{
    class ApSnmpException : Exception
    {
        public ApSnmpException(string msg)
            : base(msg)
        {
        }
    }
    class ApSnmpNoIpException : ApSnmpException
    {
        public ApSnmpNoIpException(string msg)
            : base(msg)
        {
        }
    }
    public class ApSnmp
    {
        DbWrapper.DbDataTypes.Ap m_apInfo = null;
        private SnmpWrapper.Snmp snmp = null;
        public ApSnmp(DbWrapper.DbDataTypes.Ap apinfo)
        {
            m_apInfo = apinfo;
        }
        public bool RestructSnmpOperator()
        {
            snmp = null;
            return true;
        }
        protected  SnmpWrapper.Snmp GetSnmpOperator()
        {
            if (snmp == null)
            {
                string strIp = 
                    ApIpPool.ApIpPool.Instance.GetIp(m_apInfo.id);
                if (strIp != null)
                {
                    string strUser = m_apInfo.snmp_user;
                    string strPassword = m_apInfo.snmp_password;
                    if (strUser.Length == 0)
                    {
                        //未托管snmp认证信息，使用默认的
                        strUser = 
                            Properties.Settings.Default.SnmpDefaultUser;
                        strPassword = 
                            Properties.Settings.Default.SnmpDefaultPassword;
                    }
                    snmp = new SnmpWrapper.Snmp(strIp,
                        strUser,
                        strPassword);
                }
                else
                {
                    throw new ApSnmpNoIpException("该AP未发现在线管理IP");
                }
            }
            return snmp;
        }
        public int ScanNumOfstaOnAp()
        {
            string strNum = GetSnmpOperator().get(".1.3.6.1.4.1.16535.1.1.0");
            return Convert.ToInt32(strNum);
        }

        public string ScanHardVersion()
        {
            return GetSnmpOperator().get(".1.3.6.1.4.1.16535.1.4.0");
        }

        public string ScanSoftwareVersion()
        {
            return GetSnmpOperator().get(".1.3.6.1.4.1.16535.1.3.0");
        }

        public string ScanSystemRunningTime()
        {
            return GetSnmpOperator().get(".1.3.6.1.4.1.16535.1.2.0");
        }

        public string ScanApMacAddr()
        {
            return GetSnmpOperator().get(".1.3.6.1.4.1.16535.1.5.0");
        }

        public string ScanApIpAddr()
        {
            return GetSnmpOperator().get(".1.3.6.1.4.1.16535.1.6.0");
        }

        public string ScanApWorkMode()
        {
            return GetSnmpOperator().get(".1.3.6.1.4.1.16535.8.1.0");
        }

        public bool SetApWorkMode(string strMode)
        {
            return GetSnmpOperator().set(".1.3.6.1.4.1.16535.8.1.0",
                Lextm.SharpSnmpLib.SnmpType.OctetString,
                strMode);
        }

        public string ScanApSSID()
        {
            return GetSnmpOperator().get(".1.3.6.1.4.1.16535.2.2.0");
        }

        public bool SetApSSID(string strSSID)
        {
            return GetSnmpOperator().set(".1.3.6.1.4.1.16535.2.2.0",
                Lextm.SharpSnmpLib.SnmpType.OctetString,
                strSSID);
        }

        public int ScanApPrimaryChannal()
        {
            string strChannal = "";
            strChannal = GetSnmpOperator().get(".1.3.6.1.4.1.16535.2.3.0");
            return Convert.ToInt32(strChannal);
        }

        public bool SetApPrimaryChannal(int nChannal)
        {
            return GetSnmpOperator().set(".1.3.6.1.4.1.16535.2.3.0",
                Lextm.SharpSnmpLib.SnmpType.Integer32,
                nChannal);
        }

        public string ScanWirelessMode()
        {
            return GetSnmpOperator().get(".1.3.6.1.4.1.16535.2.4.0");
        }

        public bool SetWirelessMode(string mode)
        {
            return GetSnmpOperator().set(".1.3.6.1.4.1.16535.2.4.0",
                Lextm.SharpSnmpLib.SnmpType.OctetString,
                mode);
        }

        public bool ScanWirelessOnBoot()
        {
            bool bEnable = true;
            string strOnBoot =
                GetSnmpOperator().get(".1.3.6.1.4.1.16535.2.1.0");
            switch (strOnBoot)
            {
                case "enable":
                    bEnable = true;
                    break;
                case "disable":
                    bEnable = false;
                    break;
                default:
                    throw new Exception("unknow value from snmp");
            }
            return bEnable;
        }

        public bool SetWirelessOnBoot(bool enable)
        {
            string strRequest = "";
            if (enable)
            {
                strRequest = "enable";
            }
            else
            {
                strRequest = "disable";
            }
            return GetSnmpOperator().set(".1.3.6.1.4.1.16535.2.1.0",
                Lextm.SharpSnmpLib.SnmpType.OctetString,
                strRequest);
        }

        public bool ScanWDS()
        {
            bool bEnable = true;
            string strWDS =
                GetSnmpOperator().get(".1.3.6.1.4.1.16535.2.6.0");
            switch (strWDS)
            {
                case "enable":
                    bEnable = true;
                    break;
                case "disable":
                    bEnable = false;
                    break;
                default:
                    throw new Exception("unknow value from snmp");
            }
            return bEnable;
        }

        public bool SetWDS(bool enable)
        {
            string strRequest = "";
            if (enable)
            {
                strRequest = "enable";
            }
            else
            {
                strRequest = "disable";
            }
            return GetSnmpOperator().set(".1.3.6.1.4.1.16535.2.6.0",
                Lextm.SharpSnmpLib.SnmpType.OctetString,
                strRequest);
        }

        public string ScanWirelessList()
        {
            return GetSnmpOperator().get(".1.3.6.1.4.1.16535.3.2.0");
        }

        public int ScanTxPower()
        {
            string strTxPower = "";
            strTxPower = GetSnmpOperator().get(".1.3.6.1.4.1.16535.4.1.0");
            return Convert.ToInt32(strTxPower);
        }

        public bool SetTxPower(int nTxPower)
        {
            return GetSnmpOperator().set(".1.3.6.1.4.1.16535.4.1.0",
                Lextm.SharpSnmpLib.SnmpType.Integer32,
                nTxPower);
        }

        public bool ScanWMM()
        {
            bool bEnable = true;
            string strWMM =
                GetSnmpOperator().get(".1.3.6.1.4.1.16535.4.2.0");
            switch (strWMM)
            {
                case "enable":
                    bEnable = true;
                    break;
                case "disable":
                    bEnable = false;
                    break;
                default:
                    throw new Exception("unknow value from snmp");
            }
            return bEnable;
        }

        public bool SetWMM(bool enable)
        {
            string strRequest = "";
            if (enable)
            {
                strRequest = "enable";
            }
            else
            {
                strRequest = "disable";
            }
            return GetSnmpOperator().set(".1.3.6.1.4.1.16535.4.2.0",
                Lextm.SharpSnmpLib.SnmpType.OctetString,
                strRequest);
        }

        public bool ScanShortGI()
        {
            bool bEnable = true;
            string strShortGI =
                GetSnmpOperator().get(".1.3.6.1.4.1.16535.4.3.0");
            switch (strShortGI)
            {
                case "enable":
                    bEnable = true;
                    break;
                case "disable":
                    bEnable = false;
                    break;
                case "None":
                    bEnable = false;
                    break;
                default:
                    throw new Exception("unknow value from snmp");
            }
            return bEnable;
        }

        public bool SetShortGI(bool enable)
        {
            string strRequest = "";
            if (enable)
            {
                strRequest = "enable";
            }
            else
            {
                strRequest = "disable";
            }
            return GetSnmpOperator().set(".1.3.6.1.4.1.16535.4.3.0",
                Lextm.SharpSnmpLib.SnmpType.OctetString,
                strRequest);
        }

        public string ScanWirelessSecureStat()
        {
            return GetSnmpOperator().get(".1.3.6.1.4.1.16535.5.1.0");
        }

        public bool SetWirelessWPASecure(string strRequest)
        {
            return GetSnmpOperator().set(".1.3.6.1.4.1.16535.5.2.0",
                Lextm.SharpSnmpLib.SnmpType.OctetString,
                strRequest);
        }

        public bool SetWirelessWEPSecure(string strRequest)
        {
            return GetSnmpOperator().set(".1.3.6.1.4.1.16535.5.3.0",
                Lextm.SharpSnmpLib.SnmpType.OctetString,
                strRequest);
        }

        public bool SetWirelessNoneSecure(string strRequest)
        {
            return GetSnmpOperator().set(".1.3.6.1.4.1.16535.5.4.0",
                Lextm.SharpSnmpLib.SnmpType.OctetString,
                strRequest);
        }

        public int ScanGroupKeyUpdateCycle()
        {
            string strCyle = 
                GetSnmpOperator().get(".1.3.6.1.4.1.16535.5.5.0");
            return Convert.ToInt32(strCyle);
        }

        public bool SetGroupKeyUpdateCycle(int nGroupCycle)
        {
            return GetSnmpOperator().set(".1.3.6.1.4.1.16535.5.5.0",
                Lextm.SharpSnmpLib.SnmpType.OctetString,
                nGroupCycle.ToString());
        }

        public string ScanLanIp()
        {
            return GetSnmpOperator().get(".1.3.6.1.4.1.16535.6.2.0");
        }

        public bool SetLanIp(string strIp)
        {
            return GetSnmpOperator().set(".1.3.6.1.4.1.16535.6.2.0",
                Lextm.SharpSnmpLib.SnmpType.OctetString,
                strIp);
        }

        public string ScanLanNetmask()
        {
            return GetSnmpOperator().get(".1.3.6.1.4.1.16535.6.3.0");
        }

        public bool SetLanNetmask(string strNetmask)
        {
            return GetSnmpOperator().set(".1.3.6.1.4.1.16535.6.3.0",
                Lextm.SharpSnmpLib.SnmpType.OctetString,
                strNetmask);
        }

        public string ScanLanGateway()
        {
            return GetSnmpOperator().get(".1.3.6.1.4.1.16535.6.4.0");
        }

        public string ScanLanDhcp()
        {
            return GetSnmpOperator().get(".1.3.6.1.4.1.16535.6.5.0");
        }

        public bool SetLanDhcp(string strRequest)
        {
            return GetSnmpOperator().set(".1.3.6.1.4.1.16535.6.5.0",
                Lextm.SharpSnmpLib.SnmpType.OctetString,
                strRequest);
        }

        public bool SetLanGateway(string strIp)
        {
            return GetSnmpOperator().set(".1.3.6.1.4.1.16535.6.4.0",
                Lextm.SharpSnmpLib.SnmpType.OctetString,
                strIp);
        }

        public string ScanApDhcp()
        {
            return GetSnmpOperator().get("");
        }

        public bool SetApDhcp(string strDhcp)
        {
   /*         return GetSnmpOperator().set(".1.3.6.1.4.1.16535.8.1.0",
                Lextm.SharpSnmpLib.SnmpType.OctetString,
                strMode);
    */
            return true;
        }

        public bool ScanLedLight()
        {
            bool bEnable = false;
            string strLedLith =
                GetSnmpOperator().get(".1.3.6.1.4.1.16535.9.1.0");
            if (strLedLith.Equals("1"))
            {
                bEnable = true;
            }
            return bEnable;
        }

        public bool SetLedLight(bool enable)
        {
            int nRequest = 0;
            if (enable)
            {
                nRequest = 1;
            }
            return GetSnmpOperator().set(".1.3.6.1.4.1.16535.9.1.0",
                Lextm.SharpSnmpLib.SnmpType.Integer32,
                nRequest);
        }

        public bool Reboot()
        {
            //发送重启指令，5秒后生效
            int nDelayReboot = 5;
            return GetSnmpOperator().set(".1.3.6.1.4.1.16535.10.1.0",
                Lextm.SharpSnmpLib.SnmpType.Integer32,
               nDelayReboot);
        }

        public bool ChLoginUserAndPasswd(string strUsername, string strPassword)
        {
            string strRequest = strUsername + "@" + strPassword;
            return GetSnmpOperator().set(".1.3.6.1.4.1.16535.10.2.0",
                Lextm.SharpSnmpLib.SnmpType.OctetString,
                strRequest);
        }

        public bool RestorFactory()
        {
            return GetSnmpOperator().set(".1.3.6.1.4.1.16535.10.3.0",
                Lextm.SharpSnmpLib.SnmpType.Integer32,
               0);
        }

        public string ScanUpdateSystem()
        {
            return GetSnmpOperator().get(".1.3.6.1.4.1.16535.10.4.0");
        }

        public bool UpdateSystem(string strRequest)
        {
            return GetSnmpOperator().set(".1.3.6.1.4.1.16535.10.4.0",
                Lextm.SharpSnmpLib.SnmpType.OctetString,
                strRequest);
        }

        public string ScanSecurePasswd()
        {
            return GetSnmpOperator().get(".1.3.6.1.4.1.16535.10.5.0");
        }

        public bool SetSecurePasswd(string strRequest)
        {
            return GetSnmpOperator().set(".1.3.6.1.4.1.16535.10.5.0",
                Lextm.SharpSnmpLib.SnmpType.OctetString,
                strRequest);
        }
    }
}
